/*
 *  通用提示
 *
 */
function m_showhint(info) {
    wx.showToast({
        title: info,
        icon: 'none'
    });
    return false;
}

function m_showcheck(title, info) {
    wx.showModal({
        title: title,
        content: info,
        showCancel: true,
        success(res) {
            if (res.confirm) {
                console.log('用户点击确定');
            } else if (res.cancel) {
                console.log('用户点击取消');
            }
        }
    });
    return '';
}
/*
 * 加载中
 */
function m_showloading(info) {
    console.log('ddddddd');
    console.log(info);
    if (info == '') {
        info = '加载中...';
    }
    wx.showLoading({
        title: info,
    });
}
/*
 * 加载中 关闭
 */
function m_hideloading() {
    wx.hideLoading();
}
/*
 *  通用跳转
 *
 */
function m_tourl(url, type) {
    console.log(url);
    console.log(type);
    if (type == 'redirectto') {
        wx.redirectTo({
            url: url
        });
    } else if (type == 'tap' || type == 'tab') {
        wx.switchTab({
            url: url
        });
    } else {
        wx.navigateTo({
            url: url
        });
    }
    return false;
}



/**
 * 处理富文本里的图片宽度自适应
 * 1.去掉img标签里的style、width、height属性
 * 2.img标签添加style属性：max-width:100%;height:auto
 * 3.修改所有style里的width属性为max-width:100%
 * 4.去掉<br/>标签
 * 5.将li圆点转为方点
 * @param html
 * @returns {void|string|*}
 */
function formatRichText(html){
    let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
      match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
      match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
      match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
      return match;
    });
    newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
      match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
      return match;
    });
    newContent = newContent.replace(/<br[^>]*\/>/gi, '');
    newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
    newContent = newContent.replace(/\<li/gi, '<li style="list-style: square; margin-bottom: 10px;"');
    newContent = newContent.replace(/\<p/gi, '<p style="margin-bottom: 10px; min-height: 20px"');
    return newContent;
  }

// 处理rich-text中使用图片
// <rich-text nodes="{{detail.usedata}}" space="ensp" catchtap="catchImage"></rich-text>
function getImgFromRichText(data){
  let imgarr = [];
  let regex = new RegExp(/<img.*?(?:>|\/>)/gi); // 匹配所有图片
  let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; // 匹配src图片
  let arrsImg = data.match(regex); // data 后台返回的富文本数据
  for (let a = 0; a < arrsImg.length; a++) {
      let srcs = arrsImg[a].match(srcReg);
      imgarr.push(srcs[1])
  }
  return imgarr
}

module.exports = {
    m_showhint,
    m_tourl,
    m_showloading,
    m_hideloading,
    m_showcheck,

    formatRichText,
    getImgFromRichText
};